//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc, Kasper og Nikolaj
//*****************************************************************************
package view.elev;

import control.ControlUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.table.DefaultTableModel;
import model.Elev;
import model.ElevAftale;
import model.ElevAftaleId;

public class AftalePanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private DefaultTableModel tabelModel;
    private final ArrayList<ElevAftale> elevAftales;
    private boolean opret;

    public AftalePanel(ControlUtil controlUtil) {

        this.controlUtil = controlUtil;
        elevAftales = new ArrayList<ElevAftale>();
        String col[] = {"Opretttelses Dato", "Titel", "Oprettet af"};
        String data[][] = null;
        tabelModel = new DefaultTableModel(data, col) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        initComponents();
        opret = false;
    }

    public void hentElever() {

        int value = vaelgElev.getSelectedIndex();
        vaelgElev.removeAllItems();
        vaelgElev.setEnabled(true);
        vaelgElev.addItem("vælg Elev");
        Elev[] el = controlUtil.getElevHandler().hentElever();
        for (int i = 0; i < el.length; i++) {
            vaelgElev.addItem(el[i]);
        }
        vaelgElev.setSelectedIndex(value);
    }

    private void opdaterElev(Object selectedItem) {
        Elev elev = (Elev) selectedItem;
        Iterator elevAftale = elev.getElevAftales().iterator();
        rydTabel();

        while (elevAftale.hasNext()) {
            ElevAftale l = (ElevAftale) elevAftale.next();
            elevAftales.add(l);
            tabelModel.insertRow(tabelModel.getRowCount(), new Object[]{l.getId().getDate(), l.getTitel(), l.getPersonale().getFornavn() + " " + l.getPersonale().getEfternavn()});
        }
    }

    private void rydTabel() {
        while (tabelModel.getRowCount() > 0) {
            tabelModel.removeRow(0);
            elevAftales.clear();
        }
    }

    private void nulstil() {
        aftale.setText("");
        titel.setText("");
        opret = false;
        aftale.setEnabled(true);
        titel.setEnabled(true);
        opretKnap.setText("Gem aftale");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vælgElevLabel = new javax.swing.JLabel();
        vaelgElev = new javax.swing.JComboBox();
        TabelScroll = new javax.swing.JScrollPane();
        aftaleTabel = new javax.swing.JTable();
        AftaleScroll = new javax.swing.JScrollPane();
        aftale = new javax.swing.JTextArea();
        opretKnap = new javax.swing.JButton();
        sletKnap = new javax.swing.JButton();
        titel = new javax.swing.JTextField();
        aftaleLabel = new javax.swing.JLabel();
        titelLabel = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(1016, 696));
        setMinimumSize(new java.awt.Dimension(1016, 696));
        setPreferredSize(new java.awt.Dimension(1016, 696));
        setLayout(null);

        vælgElevLabel.setText("Vælg Elev:");
        add(vælgElevLabel);
        vælgElevLabel.setBounds(10, 20, 160, 14);

        vaelgElev.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elev1", "Elev2", "Elev3", "Elev4" }));
        vaelgElev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgElevActionPerformed(evt);
            }
        });
        add(vaelgElev);
        vaelgElev.setBounds(10, 40, 163, 20);

        aftaleTabel.setModel(tabelModel);
        aftaleTabel.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        aftaleTabel.setShowHorizontalLines(true);
        aftaleTabel.getTableHeader().setReorderingAllowed(false);
        aftaleTabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aftaleTabelMouseClicked(evt);
            }
        });
        TabelScroll.setViewportView(aftaleTabel);

        add(TabelScroll);
        TabelScroll.setBounds(10, 80, 1000, 310);

        aftale.setColumns(20);
        aftale.setRows(5);
        AftaleScroll.setViewportView(aftale);

        add(AftaleScroll);
        AftaleScroll.setBounds(150, 420, 400, 140);

        opretKnap.setText("Gem aftale");
        opretKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretKnapActionPerformed(evt);
            }
        });
        add(opretKnap);
        opretKnap.setBounds(590, 440, 110, 50);

        sletKnap.setText("Slet aftale");
        sletKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletKnapActionPerformed(evt);
            }
        });
        add(sletKnap);
        sletKnap.setBounds(590, 503, 110, 50);
        add(titel);
        titel.setBounds(10, 420, 130, 20);

        aftaleLabel.setText("Aftale:");
        add(aftaleLabel);
        aftaleLabel.setBounds(150, 400, 80, 14);

        titelLabel.setText("Titel");
        add(titelLabel);
        titelLabel.setBounds(10, 400, 60, 14);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgElevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgElevActionPerformed

        if (vaelgElev.getItemCount() != 0 && vaelgElev.getSelectedIndex() != 0) {
            opdaterElev(vaelgElev.getSelectedItem());
        } else {
            rydTabel();
        }
    }//GEN-LAST:event_vaelgElevActionPerformed

    private void opretKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretKnapActionPerformed

        if (opret == true) {
        } else {
            Elev elev = (Elev) vaelgElev.getSelectedItem();
            
            ElevAftale ea = new ElevAftale(new ElevAftaleId(elev.getCpr(), new Date(), aftale.getText()), elev, controlUtil.getBruger(), titel.getText());
            controlUtil.getElevHandler().nyAftale(ea);
            hentElever();
            opdaterElev(vaelgElev.getSelectedItem());
        }
        nulstil();
    }//GEN-LAST:event_opretKnapActionPerformed

    private void sletKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sletKnapActionPerformed

        if (aftaleTabel.getSelectedRow() >= 0) {
            controlUtil.getElevHandler().sletAftale(elevAftales.get(aftaleTabel.getSelectedRow()));
            hentElever();
            opdaterElev(vaelgElev.getSelectedItem());
        }
        nulstil();
    }//GEN-LAST:event_sletKnapActionPerformed

    private void aftaleTabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aftaleTabelMouseClicked
        opret = true;
        opretKnap.setText("Opret ny aftale");
        aftale.setText(elevAftales.get(aftaleTabel.getSelectedRow()).getId().getAftale().toString());
        titel.setText(elevAftales.get(aftaleTabel.getSelectedRow()).getTitel().toString());
        aftale.setEnabled(false);
        titel.setEnabled(false);
    }//GEN-LAST:event_aftaleTabelMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane AftaleScroll;
    private javax.swing.JScrollPane TabelScroll;
    private javax.swing.JTextArea aftale;
    private javax.swing.JLabel aftaleLabel;
    private javax.swing.JTable aftaleTabel;
    private javax.swing.JButton opretKnap;
    private javax.swing.JButton sletKnap;
    private javax.swing.JTextField titel;
    private javax.swing.JLabel titelLabel;
    private javax.swing.JComboBox vaelgElev;
    private javax.swing.JLabel vælgElevLabel;
    // End of variables declaration//GEN-END:variables
}
